package cwinsor.raking.year2014;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import cwinsor.raking.year2014.PROP_E;


public class RakerAbstractTableModel extends AbstractTableModel {

	RakerTable rakerTable;
	private static final long serialVersionUID = 1L;

	// map from column number to PROPERTY
	private Map<Integer,PROP_E> column =
			new HashMap<Integer, PROP_E>();


			/**
			 * constructor
			 */
			public RakerAbstractTableModel() {
				rakerTable = new RakerTable();
				createTableMap();
			}

			/*
			 * createColumns
			 * establish defacto contents and order of the table
			 */
			public void createTableMap() {
				Integer col = 0;
				addColumn( col++, 10, PROP_E.FIRST_NAME);
				addColumn( col++, 10, PROP_E.LAST_NAME);
				addColumn( col++, 10, PROP_E.POWER);
				addColumn( col++, 10, PROP_E.HAPPINESS);

				for (int i=0; i<PROP_E.curPrefsList().length; i++) {
					addColumn( col++,  10,  PROP_E.teamAssignmentList()[i]);
					addColumn( col++,   2,  PROP_E.curPrefsList()[i]);
					addColumn( col++,   2,  PROP_E.lastPrefsList()[i]);
				}

				if (dL >= 1) {
					Msg.note(dS + ": getColumnCount = " + getColumnCount());
				}
			}


			public void readDataFromFile(File file) {
				rakerTable.readDataFromCsvFile(file);
			}

			public void writeDataToFile(File file) {
				rakerTable.writeDataToCsvFile(file);
			}

			public void updateSchedulePreferencesFromFile(File file) {
				rakerTable.updateSchedulePreferencesFromCsvFile(file);
			}

			/*
			 * getRowCount
			 * required by AbstractTableModel
			 * return the number of rows
			 */
			public int getRowCount() {
				return rakerTable.getRowCount();
			}

			/*
			 * getColumnCount
			 * required by AbstractTableModel
			 * return the number of columns
			 * this essentially defines the number of columns
			 */
			public int getColumnCount() {
				return column.size();
			}

			/*
			 * get column name (header)
			 */
			public String getColumnName(int col) {
				if (dL > 5) {
					Msg.note(dS + ": ZONA getting column name for col " + col);
				}
				PROP_E prop = propFromCol(col);
				if (dL >= 10) {
					Msg.note(dS + ": which has name " + prop.name());
				}
				return prop.name();
			}

			/*
			 * get column class
			 * for purposes of rendering - return an example (prototype) of the object
			 */
			public Class getColumnClass(int c) {
				PROP_E prop = propFromCol(c);
				return prop.prototype().getClass();
			}

			/*
			 * add an entry in the map from column number to PROPERTY
			 */
			public void addColumn(Integer col, Integer width, PROP_E propE) {
				//		Msg.note(dS + ": ZONA addin column " + col + " -> property " + propE);
				column.put(col, propE);

				//		TableColumn tableColumn = getColumn(col);
				//	  	        column.setPreferredWidth(100); //third colu

			}

			/*
			 * propFromCol
			 * this returns a property based on column number
			 */
			public PROP_E propFromCol(int col) {
				return column.get(col);
			}


			/*
			 * required by AbstractTableModel
			 * return the value at (row,column)
			 */
			public Object getValueAt(int row, int col) {

				PROP_E prop = propFromCol(col);
				return rakerTable.getValueAt(row, prop);
			}


		


			/**
			 * debug string
			 */
			String dS = this.getClass().getSimpleName();
			Integer dL = 10;
}
